﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var sql = "select * from Articles ";

            var res = DBHelper.GetDataTable(sql);

            dataGridView1.DataSource = res;

            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dataGridView1.AllowUserToAddRows = false;

        }


        //查询
        private void button1_Click(object sender, EventArgs e)
        {
            var name = txtName.Text;

            var sql = string.Format("select * from Articles where Author like '%{0}%'",name);

            var data = DBHelper.GetDataTable(sql);

            dataGridView1.DataSource = data;
        }


        //更新
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;

                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;

                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;

                SaveOrUpdataForm saveOrUpdataForm = new SaveOrUpdataForm(id,title,author);

                var res = saveOrUpdataForm.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    var name = txtName.Text;

                    var sql = string.Format("select * from Articles where Author like '%{0}%'", name);

                    var data = DBHelper.GetDataTable(sql);

                    dataGridView1.DataSource = data;
                }
            }
            else
            {
                MessageBox.Show("请选择一行更新","提示");
            }
        }


        //添加
        private void button3_Click(object sender, EventArgs e)
        {
            SaveOrUpdataForm saveOrUpdata = new SaveOrUpdataForm();

            var res = saveOrUpdata.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var name = txtName.Text;

                var sql = string.Format("select * from Articles where Author like '%{0}%'", name);

                var data = DBHelper.GetDataTable(sql);

                dataGridView1.DataSource = data;
            }

        }


        //删除
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var id = (int)row.Cells["Id"].Value;

                var ch = MessageBox.Show("是否删除","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information);

                if (ch.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);

                    var count = DBHelper.AddOrSaveOrDelete(sql);

                    if (count > 0)
                    {
                        var name = txtName.Text;

                        var selectSql = string.Format("select * from Articles where Author like '%{0}%'", name);

                        var data = DBHelper.GetDataTable(selectSql);

                        dataGridView1.DataSource = data;

                        MessageBox.Show("删除成功", "提示");
                    }
                }

            }
            else
            {
                MessageBox.Show("请选择要删除的行","提示");
            }
        }
    }
}
